﻿using CatUtils.Utils.Currency;
using CatUtilsDll.Utils.Currency.WebUtils;
using Newtonsoft.Json;
using RabbitDownload.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace RabbitDownload.WebSite.Konachan
{
    internal class KonachanHelper
    {
        /// <summary>
        /// 获取Json并且处理成正确的格式
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetJson(string url)
        {
            String SHtml = WebHelper.HttpPcGet(url);

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(SHtml);

                string jsonText = Regex.Replace(JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None), "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase);
                jsonText = Regex.Replace(jsonText, @"\?xml", "xml");
                return jsonText;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return "";
            }
        }

        /// <summary>
        /// 判断tag是否需要屏蔽
        /// </summary>
        /// <param name="tags"></param>
        /// <returns></returns>
        public static bool GetJudgeTags(string tags)
        {
            List<string> TagList = new List<string>();
            try
            {
                foreach (string line in Regex.Split(tags, " ").ToList<string>())
                {
                    if (line != "")
                    {
                        TagList.Add(line.Trim());
                    }
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return ListHelper.CompareRepetition(TagList, StaticValueOld.ExcludeList);
        }

        /// <summary>
        /// 获取扫描用模式
        /// </summary>
        /// <param name="rating"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        public static bool GetModeFlag(string rating, int score)
        {
            switch (KonachanStaticValue.DownLoadMode)
            {
                case "仅下载安全":
                    return SafeMode(rating, score);

                case "安全+擦边球":
                    return QuestionableMode(rating, score);

                case "全部下载（含色情）":
                    return ExplicitMode(rating, score);
            }
            return true;
        }

        /// <summary>
        /// 全部下载，包括色情
        /// </summary>
        /// <param name="rating"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        private static bool ExplicitMode(string rating, int score)
        {
            return CompreedScore(score);
        }

        /// <summary>
        /// 擦边球/可疑模式
        /// </summary>
        /// <param name="rating"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        private static bool QuestionableMode(string rating, int score)
        {
            if (rating != "s" || rating != "q")
                return false;

            return CompreedScore(score);
        }

        /// <summary>
        /// 安全模式
        /// </summary>
        /// <param name="rating"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        private static bool SafeMode(string rating, int score)
        {
            if (rating != "s")
                return false;

            return CompreedScore(score);
        }

        /// <summary>
        /// 对比分数，返回结果
        /// </summary>
        /// <param name="score"></param>
        /// <returns></returns>
        private static bool CompreedScore(int score)
        {
            if (score != 0 && score < KonachanStaticValue.ImgScore)
            {
                Console.WriteLine("评分{0}小于预设{1}，需要跳过", score, KonachanStaticValue.ImgScore);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 初始化JSON，防止错乱
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static List<KonchanJsonClass.PostItem> InitPostJson(string json)
        {
            List<KonchanJsonClass.PostItem> GetImgInfo = new List<KonchanJsonClass.PostItem>();
            try
            {
                KonchanJsonClass.Root AllJson = JsonConvert.DeserializeObject<KonchanJsonClass.Root>(json);
                GetImgInfo = AllJson.posts.post;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
                try
                {
                    KonachanSingleImgJson.Root Singlejson = JsonConvert.DeserializeObject<KonachanSingleImgJson.Root>(json);
                    GetImgInfo.Add(Singlejson.posts.post);
                    Console.WriteLine("启用单个匹配模式" + GetImgInfo.Count);
                }
                catch (Exception jsex)
                {
                    PrintLog.Log(json);
                    PrintLog.Log(jsex);
                }
            }

            return GetImgInfo;
        }
    }
}